﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QXBook.Model;
using System.Data.Common;
using System.Data.SqlClient;
using QXBook.Utils;

namespace QXBook.Service
{
    public class ScoreRecordsService : ServiceBase
    {
        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <param name="AdminName"></param>
        /// <param name="article"></param>
        /// <returns></returns>
        public string AddScoreRecord(string AdminName, ScoreRecordsInfo record)
        {
            MembersInfo admininfo = ServiceFactory.CreateInstance<AdminService>().GetAdminEntityByAdminName(AdminName);
            //判断当前管理员是否存在
            string rtmessage = ServiceFactory.CreateInstance<AdminService>().CheckAdminPower(admininfo);
            if (!string.IsNullOrEmpty(rtmessage)) return rtmessage;

            DbCommand cmd = new SqlCommand();
            cmd.CommandText = @"INSERT INTO ScoreRecords(UserID,ActionStr,Payment,Description,AddDate) 
                                VALUES (@UserID,@ActionStr,@Payment,@Description,GETDATE());SELECT @@IDENTITY";//返回主键

            base.db.AddInParameter(cmd, "@UserID", DbType.Int32, record.UserID);
            base.db.AddInParameter(cmd, "@ActionStr", DbType.String, record.ActionStr);
            base.db.AddInParameter(cmd, "@Payment", DbType.Decimal, record.Payment);
            base.db.AddInParameter(cmd, "@Description", DbType.String, record.Description);

            base.db.ExecuteNonQuery(cmd);
            return string.Empty;
        }

        /// <summary>
        /// 获取荷包记录
        /// </summary>
        /// <param name="ps"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<ScoreRecordsInfo> GetModelList(ref PagerState ps, ScoreRecordCondition condition)
        {
            List<ScoreRecordsInfo> list = new List<ScoreRecordsInfo>();
            DbCommand cmd = new SqlCommand();

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("SELECT RecordID,UserID,ActionStr,Payment,Description,AddDate FROM ScoreRecords ");
            sbSql.Append(" WHERE 1=1");
            if (condition != null)
            {
                if (condition.UserID > 0)
                {
                    sbSql.Append(" AND UserID=" + condition.UserID);
                }
            }

            DataTable dt = this.GetListPager(ref ps, sbSql.ToString(), "RecordID", cmd);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ScoreRecordsInfo record = new ScoreRecordsInfo();
                    record.RecordID = CommonUtils.ToInt(dr["RecordID"]);
                    record.UserID = CommonUtils.ToInt(dr["UserID"]);
                    record.ActionStr = CommonUtils.ToString(dr["ActionStr"]);
                    record.Payment = CommonUtils.ToDecimal(dr["Payment"]);
                    record.Description = CommonUtils.ToString(dr["Description"]);
                    record.UserID = CommonUtils.ToInt(dr["UserID"]);
                    record.AddDate = CommonUtils.ToTime(dr["AddDate"].ToString());
                    list.Add(record);
                }
            }

            return list;
        }
    }
}
